package com.sdp.khy.baotou.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.utils.UniqueCheckTools;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.khy.baotou.cache.clean.KhyCleanCache;
import com.sdp.khy.baotou.db.khy_bus.entity.KhyBus;
import com.sdp.khy.baotou.db.khy_bus.service.IKhyBusService;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2023-11-29
 */
@RestController
@RequestMapping("/KhyBusController")
public class KhyBusController {

	@Autowired
	private IKhyBusService iKhyBusService;
	
	@Autowired
	private Query query;
	
	@Autowired
	private UniqueCheckTools uniqueCheckTools;
	
	@Autowired
	private KhyCleanCache khyCleanCache;
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iKhyBusService, KhyBus.class, dataCenter));
	}
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody KhyBus bus) {
		if(bus==null) {
			return Result.error("车辆信息为空");
		}
		if(StringUtils.isEmpty(bus.getKeyId())) {
			Result<?> r = uniqueCheckTools.isUniqueSingle(iKhyBusService, KhyBus.class, new String[] {"plateNo"}, "keyId", bus, null, false);
			if(r==null || !r.isSuccess()) {
				return Result.error("车牌号不能重复");
			}
			
			iKhyBusService.save(bus);
		}else {
			Result<?> r = uniqueCheckTools.isUniqueSingle(iKhyBusService, KhyBus.class, new String[] {"plateNo"}, "keyId", null, bus, false);
			if(r==null || !r.isSuccess()) {
				return Result.error("车牌号不能重复");
			}
			iKhyBusService.updateById(bus);
		}
		khyCleanCache.cleanBusCache();
		return Result.ok();
	}
	
	@GetMapping(value = "/del")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> del(@RequestParam(name="keyId",required=true) String keyId) {
		if(StringUtils.isEmpty(keyId)) {
			return Result.error("需要删除的记录信息不完整");
		}
		iKhyBusService.removeById(keyId);
		khyCleanCache.cleanBusCache();
		return Result.ok();
	}
}

